package com.ytc.content.service.impl;

import cn.hutool.core.util.RandomUtil;

import com.ytc.content.config.SmsTemplate;
import com.ytc.content.service.CommonSmsMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CommonSmsMsgServiceImpl implements CommonSmsMsgService {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void sms(String phone) {
       //1. 生成6位数验证码
        String code = RandomUtil.randomNumbers(6);

        //String code = "123";
        System.out.println("生成的验证码是:" + code);

        //2. 调用reids保存一下
        redisTemplate.opsForValue().set("CODE_" + phone, code, 5, TimeUnit.MINUTES);

        //3. 调用阿里云发送
        smsTemplate.sendSms(phone, code);
    }
}
